package com.mediacloud.app.newsmodule.fragment.navlive

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.assembly.util.DateParse
import com.mediacloud.app.model.news.ProgramListItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import com.utils.read.ReadPlayView4
import java.text.SimpleDateFormat
import java.util.*

class NavLiveProgramListAdapter(context: Context?) :
    BaseRecyclerAdapterX<ProgramListItem>(context) {

    var chooseIndex = -1
    var livingIndex = -1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ProgramListHolder(
            LayoutInflater.from(getContext())
                .inflate(R.layout.aappfactory_itemprogram1, parent, false)
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is ProgramListHolder) {
            holder.update(getItem(position), chooseIndex == position)
        }
    }

    inner class ProgramListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val c1 = ContextCompat.getColor(itemView.context, R.color.cctv7_red_light)
        val c2 = ContextCompat.getColor(itemView.context, R.color.c_222)
        val c3 = ContextCompat.getColor(itemView.context, R.color.color999999)
        val time: TextView = itemView.findViewById(R.id.time)
        val programName: TextView = itemView.findViewById(R.id.programName)
        val state: ImageView = itemView.findViewById(R.id.tv_back_booking)
        val readPlay: ReadPlayView4 = itemView.findViewById(R.id.readPlay)
        val playView: View = itemView.findViewById(R.id.playView)
        private val dateFormat = SimpleDateFormat("HH:mm", Locale.CHINA)

        fun update(item: ProgramListItem, isSelected: Boolean) {
            val guideItemState = ProgramListItem.getItemState(item)
            when (guideItemState!!) {
                ProgramListItem.GuideItemState.REPLAY -> {

                    if (isSelected) {
                        state.visibility = View.INVISIBLE
                        readPlay.start()
                        playView.visibility = View.VISIBLE
                        state.setImageResource(R.drawable.tv_pro_list_1)
                        time.setTextColor(c1)
                        programName.setTextColor(c1)

                    } else {
                        state.visibility = View.VISIBLE
                        readPlay.pause()
                        playView.visibility = View.GONE
                        state.setImageResource(R.drawable.tv_pro_list_3)
                        time.setTextColor(c3)
                        programName.setTextColor(c2)
                    }

                }
                ProgramListItem.GuideItemState.LIVE -> {

                    if (isSelected) {
                        state.visibility = View.INVISIBLE
                        readPlay.start()
                        playView.visibility = View.VISIBLE
                        state.setImageResource(R.drawable.tv_pro_list_1)
                        time.setTextColor(c1)
                        programName.setTextColor(c1)
                    } else {
                        state.visibility = View.VISIBLE
                        readPlay.pause()
                        playView.visibility = View.GONE
                        state.setImageResource(R.drawable.tv_pro_list_2)
                        time.setTextColor(c3)
                        programName.setTextColor(c2)
                    }
                }
                ProgramListItem.GuideItemState.COMING_SOON -> {
                    state.visibility = View.VISIBLE
                    readPlay.pause()
                    playView.visibility = View.GONE
                    state.setImageResource(R.drawable.tv_pro_list_4)
                    time.setTextColor(c3)
                    programName.setTextColor(c2)
                }

                ProgramListItem.GuideItemState.APPOINTMENTED -> {
                    state.visibility = View.VISIBLE
                    readPlay.pause()
                    playView.visibility = View.GONE
                    state.setImageResource(R.drawable.tv_pro_list_5)
                    time.setTextColor(c3)
                    programName.setTextColor(c2)

                }
            }
            programName.text = item.name
            try {
                val timeTxt =
                    dateFormat.format(DateParse.mDateFormat.parse(item.starttime)) + "-" + dateFormat.format(DateParse.mDateFormat.parse(item.endtime))
                time.text = timeTxt
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }
}